<?php
/**
 * @author qingyu8@staff.weibo.com
 * @date 2018/08/18
 */
namespace api\modules\mp\models;

use common\tool\Curd;
use yii\base\Model;
use yii\web\NotFoundHttpException;

class Companion extends Model {
    use Curd;

    public static function tableName() {
        return 'mp_companion';
    }

    public function rules() {
        return [
            [
                [
                    'title',
                    'stime',
                    'etime',
                    'place'
                ],
                'required'
            ],
            [
                [
                    'title',
                    'description',
                    'pics',
                    'openid'
                ],
                'string'
            ],
            [
                [
                    'stime',
                    'etime',
                    'ctime',
                    'utime',
                ],
                'datetime',
                'format' => 'yyyy-MM-dd HH:mm:ss'
            ],
            [
                'object',
                'in','range'=>[self::OBJECT_FEMALE,self::OBJECT_MALE,self::OBJECT_NONE],
            ],
            [
                'status',
                'in',
                'range' => [
                    self::STATUS_DRAFT,self::STATUS_PUBLISH,self::STATUS_CANCEL
                ],
            ],
        ];
    }

    public static function build($data = []): ?self {
        if (isset($data['id'])) {
            $one = self::getOne("id={$data['id']}");
            if (!$one) throw new NotFoundHttpException();
            $data = array_merge($one, $data);
        }
        return new self($data);
    }

    public $id;
    public $title;
    public $openid;
    public $stime;
    public $etime;
    public $description = '';
    public $place;
    public $object = self::OBJECT_NONE;
    public $pics = '';
    public $ctime;
    public $utime;
    public $status = self::STATUS_PUBLISH;
    const OBJECT_MALE = 1;
    const OBJECT_FEMALE = 2;
    const OBJECT_NONE = 0;//不限制约伴对象
    const STATUS_DRAFT = 1;
    const STATUS_PUBLISH = 2;
    const STATUS_CANCEL = 3;
    const STATUS_COMPLETE = 4;

    public function setStatus($s) {
        if (in_array($s, [
            self::STATUS_DRAFT,
            self::STATUS_PUBLISH,
            self::STATUS_CANCEL
        ])) $this->status = (int)$s;
    }

    public function setObject($object) {
        if (in_array($object, [
            self::OBJECT_NONE,
            self::OBJECT_MALE,
            self::OBJECT_FEMALE
        ])) $this->object = (int)$object;
    }

    public function setPics($pics) { $this->pics = $pics; }

    public function save() {
        $data = [
            'title' => $this->title,
            'openid' => $this->openid,
            'stime' => $this->stime,
            'etime' => $this->etime,
            'description' => $this->description,
            'place' => $this->place,
            'object' => $this->object,
            'pics' => $this->pics,
            'status' => $this->status,
        ];
        $now = date('Y-m-d H:i:s');
        if ($this->id) {
            $data['utime'] = $now;
            self::update($data, 'id=:id', [':id' => $this->id]);
        } else {
            $data['ctime'] = $data['utime'] = $now;
            $id = self::insert($data);
            $this->id = $id;
        }
    }

    public function publish() {
        return self::update(['status' => self::STATUS_PUBLISH], 'id=:id', [':id' => $this->id]);
    }

    public function complete() {
        return self::update(['status' => self::STATUS_COMPLETE], 'id=:id', [':id' => $this->id]);
    }

    public function cancel() {
        return self::update(['status' => self::STATUS_CANCEL], 'id=:id', [':id' => $this->id]);
    }

    public function info() {
        return $this->getAttributes();
    }
}
